package com.stationbuilding.adapter.home;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.stationbuilding.MyApplication;
import com.stationbuilding.R;
import com.stationbuilding.bean.QuestionTypeBean;

import java.util.List;

public class HomeListAdapter extends BaseAdapter {

    private Context mContext;
    private List<QuestionTypeBean> questionTypeList;

    public HomeListAdapter(@NonNull List<QuestionTypeBean> list) {
        this.mContext = MyApplication.getContext();
        this.questionTypeList = list;
    }

    public void setList(List<QuestionTypeBean> list) {
        this.questionTypeList = list;
    }

    @Override
    public int getCount() {
        return questionTypeList.size();
    }

    @Override
    public Object getItem(int position) {
        return questionTypeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        QuestionTypeBean questionTypeBean = questionTypeList.get(position);
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.home_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.type = (TextView) convertView.findViewById(R.id.type);
            viewHolder.count = (TextView) convertView.findViewById(R.id.count);
            viewHolder.score = (TextView) convertView.findViewById(R.id.score);
            viewHolder.countProgressBar = (ProgressBar) convertView.findViewById(R.id.count_progress_bar);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.type.setText(questionTypeBean.getName() + "：");
        viewHolder.count.setText(String.valueOf(questionTypeBean.getCount()));
        viewHolder.score.setText(String.valueOf(questionTypeBean.getScore()));
        viewHolder.countProgressBar.setMax(questionTypeList.get(0).getCount());
        viewHolder.countProgressBar.setProgress(questionTypeBean.getCount());
        return convertView;
    }

    private static class ViewHolder {
        private TextView type;
        private TextView count;
        private TextView score;
        private ProgressBar countProgressBar;
    }
}
